/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guide.task;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.CacheUtils;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.RedisUtils;
import com.jeeplus.common.utils.time.DateUtil;
import com.jeeplus.modules.guide.entity.*;
import com.jeeplus.modules.guide.service.DockingOtherService;
import com.jeeplus.modules.guide.service.MobileLoginService;
import com.jeeplus.modules.guide.service.MobileOtherService;
import com.jeeplus.modules.guide.service.MobileQueueService;
import com.jeeplus.modules.sys.utils.FileKit;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.work.queue.entity.ElectronicMaterialEntity;
import com.jeeplus.modules.work.queue.service.ElectronicMaterialService;
import com.jeeplus.modules.work.queue.service.ManageDurationService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import okhttp3.*;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/*
第三方对接接口
*/
@Component

public class ManageDuration{
    @Autowired
    private ManageDurationService service;

    private static final Logger log = LoggerFactory.getLogger(ManageDuration.class);

    public AjaxJson getManageDurationTime(Boolean flag){
        Object regionalData = CacheUtils.get("regionalData", "regionalvalue");
        Object streetData = CacheUtils.get("streetData", "streetvalue");
        if(flag == true || null == regionalData || null == streetData){
            List <Map <String, Object>> regionalDatas = service.getRegionalLastMonth().stream().map(e -> {
                if ("工程建设".equals(e.get("name"))) {
                    e.put("doTime", 28);
                }
                return e;
            }).collect(Collectors.toList());
            List <Map <String, Object>> streetDatas = service.getStreetLastMonth().stream().map(e -> {
                if ("合作街道".equals(e.get("areaName"))) {
                    e.put("doTime", 45);
                }
                e.put("name", e.remove("areaName"));
                return e;
            }).collect(Collectors.toList());
            CacheUtils.put("regionalData", "regionalvalue",regionalDatas);
            CacheUtils.put("streetData", "streetvalue", streetDatas);
            return AjaxJson.success().put("regionalData",regionalDatas).put("streetData",streetDatas);
        }else{
            return AjaxJson.success().put("regionalData",regionalData).put("streetData",streetData);
        }
    }

    public JSONArray getManageDurationValue(){
        JSONArray regional=new JSONArray();
        List<Map<String,Object>> regionalToday = service.getRegionalToday();
        List<Map<String,Object>> regionalLastMonth = service.getRegionalLastMonth();
        List<Map<String,Object>> streetToday = service.getStreetToday();
        List<Map<String,Object>> streetLastMonth = service.getStreetLastMonth();

//        遍历对比当天平均办件是否超过预警值,街道上月*1.4,区级上月*1.4
        for (int i = 0; i < regionalToday.size(); i++) {
            for (int k = 0; k < regionalLastMonth.size(); k++) {
                if("工程建设".equals(regionalLastMonth.get(k).get("name"))){
                    regionalLastMonth.get(k).put("doTime","28");
                }
                if (regionalToday.get(i).get("name").equals(regionalLastMonth.get(k).get("name"))){
                    if(Double.parseDouble(regionalToday.get(i).get("doTime").toString())>Double.parseDouble(regionalLastMonth.get(k).get("doTime").toString())){
                        JSONObject map =new JSONObject();
                        map.put("id",UUID.randomUUID().toString());
                        map.put("name",regionalToday.get(i).get("name"));
                        map.put("doTime",regionalToday.get(i).get("doTime"));
                        map.put("time",DateUtils.getDateTime());
                        map.put("prewarningValue",Double.parseDouble(regionalLastMonth.get(k).get("doTime").toString()));
                        regional.add(map);
                    }
                }
            }
        }
        for (int i = 0; i < streetToday.size(); i++) {
            for (int k = 0; k < streetLastMonth.size(); k++) {
                if("合作街道".equals(streetLastMonth.get(k).get("areaName"))){
                    streetLastMonth.get(k).put("doTime","45");
                }
                if (streetToday.get(i).get("areaName").equals(streetLastMonth.get(k).get("areaName"))){
                    if(Double.parseDouble(streetToday.get(i).get("doTime").toString())>Double.parseDouble(streetLastMonth.get(k).get("doTime").toString())){
                        JSONObject map =new JSONObject();
                        map.put("id",UUID.randomUUID().toString());
                        map.put("name",streetToday.get(i).get("areaName"));
                        map.put("doTime",streetToday.get(i).get("doTime"));
                        map.put("time",DateUtils.getDateTime());
                        map.put("prewarningValue",Double.parseDouble(streetLastMonth.get(k).get("doTime").toString()));
                        regional.add(map);
                    }
                }
            }
        }
        return regional;
    }

    public void getManageDuration(){
            JSONArray regional=new JSONArray();
            List<Map<String,Object>> regionalToday = service.getRegionalToday();
            List<Map<String,Object>> regionalLastMonth = service.getRegionalLastMonth();
            List<Map<String,Object>> streetToday = service.getStreetToday();
            List<Map<String,Object>> streetLastMonth = service.getStreetLastMonth();

//        遍历对比当天平均办件是否超过预警值,街道上月*1.4,区级上月*1.4
            for (int i = 0; i < regionalToday.size(); i++) {
                for (int k = 0; k < regionalLastMonth.size(); k++) {
                    if("工程建设".equals(regionalLastMonth.get(k).get("name"))){
                        regionalLastMonth.get(k).put("doTime","28");
                    }
                    if (regionalToday.get(i).get("name").equals(regionalLastMonth.get(k).get("name"))){
                        if(Double.parseDouble(regionalToday.get(i).get("doTime").toString())>Double.parseDouble(regionalLastMonth.get(k).get("doTime").toString())){
                            JSONObject map =new JSONObject();
                            map.put("id",UUID.randomUUID().toString());
                            map.put("name",regionalToday.get(i).get("name"));
                            map.put("doTime",regionalToday.get(i).get("doTime"));
                            map.put("time",DateUtils.getDateTime());
                            map.put("prewarningValue",Double.parseDouble(regionalLastMonth.get(k).get("doTime").toString()));
                            regional.add(map);
                        }
                    }
                }
            }
            for (int i = 0; i < streetToday.size(); i++) {
                for (int k = 0; k < streetLastMonth.size(); k++) {
                    if("合作街道".equals(streetLastMonth.get(k).get("areaName"))){
                        streetLastMonth.get(k).put("doTime","45");
                    }
                    if (streetToday.get(i).get("areaName").equals(streetLastMonth.get(k).get("areaName"))){
                        if(Double.parseDouble(streetToday.get(i).get("doTime").toString())>Double.parseDouble(streetLastMonth.get(k).get("doTime").toString())){
                            JSONObject map =new JSONObject();
                            map.put("id",UUID.randomUUID().toString());
                            map.put("name",streetToday.get(i).get("areaName"));
                            map.put("doTime",streetToday.get(i).get("doTime"));
                            map.put("time",DateUtils.getDateTime());
                            map.put("prewarningValue",Double.parseDouble(streetLastMonth.get(k).get("doTime").toString()));
                            regional.add(map);
                        }
                    }
                }
            }
            for (int i = 0; i < regional.size(); i++) {
                if(service.findWarning(regional.getJSONObject(i).getString("name"),regional.getJSONObject(i).getString("time")) <= 0){
                    service.intoWarning(regional.getJSONObject(i).getString("id"),regional.getJSONObject(i).getString("name"),regional.getJSONObject(i).getString("doTime"),regional.getJSONObject(i).getString("prewarningValue"),regional.getJSONObject(i).getString("time"));
                    sendManageDuration(regional.getString(i));
                }
            }
        }
        public void sendManageDuration(String regional){
            JSONObject object = JSONObject.fromObject(regional);
            log.error("推送办件时长预警区域或街道:"+object);
            Response response =null;
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, object.toString());
            Request request = new Request.Builder()
                    .url("https://gxzs.cdyoue.com/apis2/table/ywtb_bjcsyj")
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .build();
            try {
                response = client.newCall(request).execute();
                log.error("推送办件时长预警区域或街道接口返回:"+response.body().string());
            } catch (Exception e) {
                log.error("推送办件时长预警区域或街道接口报错 ："+e.getMessage());
            }finally{
                if (null != response) {
                    try {
                        response.close();
                    } catch (Exception e) {
                        System.err.println("释放连接出错");
                        e.printStackTrace();
                    }
                }
            }
        }


    public List<Map<String,Object>> departmentList(){
        return  service.departmentList();
    }

}
